//index.js  
//获取应用实例  
var app = getApp()
Page({
  data: {
    /** 
        * 页面配置 
        */
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,
    imgUrls: [
      app.myUlife.ulifeUrl + '/wx/topimg2.jpg',
    ],
    imgdefalut: app.myUlife.ulifeUrl + '/wx/topimg2.jpg',
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 1200,
    ulifeUrl: app.myUlife.ulifeUrl,//服务器路径
    swiperh: [10, 10, 10],//swiper高度
    swiperheight: 0,
    jj:''
  },
  onLoad: function () {
    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    this.getSchoolDetail();//获取图片
    this.getsystemset();
  },
  // 获取学校简介
  getSchoolDetail: function () {
    var that = this;
    var csid = wx.getStorageSync('csid');
    wx.request({
      url: app.myUlife.ulifeUrl +'/wechart/getSchoolDetail.html', //仅为示例，并非真实的接口地址
      data: {
        csid: csid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          jj: res.data.JIANJIE
        });
      }
    })
  },

  //加载顶部图片
  getsystemset: function (e) {
    var csid = wx.getStorageSync('csid');
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + 'getsystemSet.html',
      data: {
        csid: csid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var imgUrls = [];
        var topimage1s = res.data.TOPIMAGE1S;
        if (topimage1s != null && topimage1s != undefined && topimage1s == "0") {
          var topimage1 = res.data.TOPIMAGE1;
          if (topimage1 != null && topimage1 != undefined) {
            imgUrls.push(app.myUlife.ulifeUrl + topimage1);
          }
        }

        var topimage2s = res.data.TOPIMAGE2S;
        if (topimage2s != null && topimage2s != undefined && topimage2s == "0") {
          var topimage2 = res.data.TOPIMAGE2;
          if (topimage2 != null && topimage2 != undefined) {
            imgUrls.push(app.myUlife.ulifeUrl + topimage2);
          }
        }

        var topimage3s = res.data.TOPIMAGE3S;
        if (topimage3s != null && topimage3s != undefined && topimage3s == "0") {
          var topimage3 = res.data.TOPIMAGE3;
          if (topimage3 != null && topimage3 != undefined) {
            imgUrls.push(app.myUlife.ulifeUrl + topimage3);
          }
        }

        that.setData({
          imgUrls: imgUrls
        })

      }
    })
  },

  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
    var swiperh = that.data.swiperh;
    that.setData({
      swiperheight: swiperh[e.detail.current]
    })
  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

 /** 
   * 自动计算图片高度 
   */
  imageLoad: function (e) {
    var that = this;
    var lindex = parseInt(e.currentTarget.dataset.lindex);
    var swiperh = that.data.swiperh;
    var fwlnh = swiperh[0];
    var fwbzh = swiperh[1];
    var ywjsh = swiperh[2];

    var winWidth = parseFloat(that.data.winWidth);//屏幕的宽
    var imageh = parseFloat(e.detail.height);//原始图片的高
    var imagew = parseFloat(e.detail.width);//原始图片的宽
    //计算缩放之后图片的高
    var ximageh = (imageh * winWidth) / imagew;

    if (lindex == "0") {
      fwlnh += ximageh; 
    } else if (lindex == "1"){
      fwbzh += ximageh;
    } else if (lindex == "2") {
      ywjsh += ximageh;
    }
    var swiperh1=[];
    swiperh1.push(fwlnh);
    swiperh1.push(fwbzh);
    swiperh1.push(ywjsh);
    that.setData({
      swiperh: swiperh1,
      swiperheight: swiperh1[0]
    })
  },


  errorFunction: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var imgUrls = this.data.imgUrls    　　　　　　　//将图片列表数据绑定到变量
      imgUrls[errorImgIndex] = this.data.imgdefalut //错误图片替换为默认图片
      this.setData({
        imgUrls: imgUrls
      })
    }
  }

})  